/*
============================================================================
 Name			: BlueToothDeviceDiscoverer.cpp
 Author	  		: 
 Copyright   	:
 Description 	: Implementation
============================================================================
*/

// INCLUDE FILES
#include <btextnotifiers.h>
#include "BluetoothDeviceDiscoverer.h"
#include "common.hrh"
/*
============================================================================
CBluetoothDeviceDiscoverer's two stage constructor
============================================================================
*/
CBluetoothDeviceDiscoverer* CBluetoothDeviceDiscoverer::NewL(MBluetoothDeviceDiscovererObserver& aObs)
	{
    CBluetoothDeviceDiscoverer* self = new (ELeave) CBluetoothDeviceDiscoverer(aObs);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop();
    return self;
	}

/*
============================================================================
CBluetoothDeviceDiscoverer's second phase constructor 
============================================================================
*/
void CBluetoothDeviceDiscoverer::ConstructL()
	{
	//Create an RNotifier object, and connect it with RNotifier::Connect()
	User::LeaveIfError(iBTDevicesNotifier.Connect()); 	
	}

/*
============================================================================
CBluetoothDeviceDiscoverer's constructor 
============================================================================
*/
CBluetoothDeviceDiscoverer::CBluetoothDeviceDiscoverer(MBluetoothDeviceDiscovererObserver& aObs) :
	CActive(CActive::EPriorityStandard), iObserver(aObs)
	{
	CActiveScheduler::Add(this);
	}

/*
============================================================================
CBluetoothDeviceDiscoverer's destructor
============================================================================
*/
CBluetoothDeviceDiscoverer::~CBluetoothDeviceDiscoverer()
	{
	Cancel();
	//After using a RNotifier object we need to close it.	
	iBTDevicesNotifier.Close();
	}
	
/*
============================================================================
DoCancel is called as part of the active object's Cancel().
Cancel the notifier.
============================================================================
*/
void CBluetoothDeviceDiscoverer::DoCancel()
	{
	//If the user dismisses the RNotifier's dialog without selecting any devices then we call
	//RNotifier::CancelNotifier with the same dialog UID (KDeviceSelectionNotifierUid) we used
	//in RNotifier::StartNotifierAndGetResponse.
	iBTDevicesNotifier.CancelNotifier(KDeviceSelectionNotifierUid);
	}


/*
============================================================================
Displays the BT devices in range for the player to choose from
============================================================================
*/
void CBluetoothDeviceDiscoverer::DiscoverAndSelectDeviceL(const TBTDeviceSelectionParamsPckg& aSelectionFilter)
	{
	if(IsActive())
		{	
		User::Leave(KErrInUse);
		}
	else
		{		
		//We ask RNotifier to start the search for other BT devices in range. Seraching for BT devices is done
		//asynchronously by calling RNotifier::StartNotifierAndGetResponse. Application can filter the search
		//of BT devices by passing a TBTDeviceSelectionParams wrapped in a TBTDeviceSelectionParamsPckg
		//pckg buffer. In our example we passed the UID of the authorisation plug-in (KDeviceSelectionNotifierUid)
		//that invokes the BT device selection dialog, the device and/or service classes by which to filter
		//suitable remote devices (RPS's service class KRPS_BTServiceID), a Bluetooth address object
		//that on completion of the dialog will hold information about the device the user selected in
		//a packaged TBTDeviceResponseParams.
		iBTDevicesNotifier.StartNotifierAndGetResponse(iStatus, KDeviceSelectionNotifierUid, aSelectionFilter, iResponse);
		SetActive();
		}
	}

/*
============================================================================
Handles CBluetoothConnector's completion events
============================================================================
*/
void CBluetoothDeviceDiscoverer::RunL()
	{
	//We pass on to the engine the BT device address the user selected otherwise we pass on the error
	if(iStatus.Int() == KErrNone)
		{
		iObserver.OnDeviceDiscoveryComplete(iResponse);
		}
	else
		{	
		iObserver.OnDeviceDiscoveryErr(iStatus.Int());	
		}
	}

/*
============================================================================
Handles a leave occurring in the request completion event handler RunL().
Reports the error to the observer
============================================================================
*/
TInt CBluetoothDeviceDiscoverer::RunError(TInt aError)
	{
	iObserver.OnDeviceDiscoveryErr(aError);
	return KErrNone;
	}
